import numpy as np

# 算术函数
a = np.arange(12, dtype=float).reshape(3, 4)
b = np.array([10, 11, 12, 13])
print('二维数组 a: ')
print(a)
print('一维数组 b: ')
print(b)
print('a+b：'.center(50, '*'))
c = np.add(a, b)
print(c)
print('a-b：'.center(50, '*'))
c = np.subtract(a, b)
print(c)
print('a*b：'.center(50, '*'))
c = np.multiply(a, b)
print(c)
print('a/b：'.center(50, '*'))
c = np.divide(a, b)
print(c)

# 数学函数
print('三角函数'.center(50, '*'))
a = np.array([0., 30, 45, 60, 90, 180, 360])
# 可以将角度数值通过乘上pi/180转换为弧度
print('不同角度的sin函数：')
print(np.sin(a * (np.pi / 180)))
print('不同角度的cos函数：')
print(np.cos(a * (np.pi / 180)))
print('不同角度的tan函数：')
print(np.tan(a * (np.pi / 180)))

a = np.array([1.0, 4.55, 123, 0.567, 25.532])
print('原数组：')
print(a)
print('round 舍入后：')
print(np.around(a))
# decimals: 舍入的小数位数。 默认值为 0。 如果为负，整数将四舍五入到小数点左侧的位置。
print(np.around(a, decimals=1))
print(np.around(a, decimals=-1))
print('floor 向下取整：')
print(np.floor(a))
print('ceil 向上取整：')

# 聚合函数
print('聚合函数'.center(50, '*'))
a = np.random.randint(1, 10, size=10)
print('原数组：')
print(a)
sum = np.sum(a)
print('求和：%s' % sum)
mid = np.median(a)
print('中数：%s' % mid)
pow = np.power(a, 2)
print('二次幂：%s' % pow)
avg = np.mean(a)
print('平均值：%s' % avg)
std = np.std(a)
print('标准差：%s' % std)
v = np.var(a)
print('方差：%s' % v)
arg_max = np.argmax(a)
print('最大值下标:%s' % arg_max)
arg_min = np.argmin(a)
print('最小值下标：%s' % arg_min)
i = np.inf
print('无穷大：%s' % i)

c = np.arange(24).reshape(3, 4, 2)
print(c)
print('以第0轴相加')
print(c.sum(axis=0))
print('以第1轴相加')
print(c.sum(axis=1))
print('以第2轴相加')
print(c.sum(axis=2))

c = np.zeros(10)
print(c)
c[4] = 1
print(c)
c = np.arange(10, 50)
print(c)
c = np.arange(0, 9).reshape(3, 3)
print(c)
c = np.arange(10).reshape(2, -1)
print(c)

print('原数组')
c = np.arange(9).reshape(3, 3)
print(c)
print('反转行')
c = c[::-1]
print(c)
print('反转列')
c = c[:, ::-1]
print(c)
print('反转行列')
c = c[::-1, ::-1]
print(c)
